package com.hy.myapp.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hy.myapp.Entity.User;
import com.hy.myapp.mapper.ReputationMapper;
import com.hy.myapp.service.ReputationService;

/**
 * Reputation控制器
 * Reputation=影星众多艺名中最广为人知的大名，如刘德华，段奕宏，他们可能还有艺名，但这两个最响亮
 * 成名作，成名曲中的成名和本系统中的Reputation最为接近。
 * 在此系统中，Reputation就是‘成名’的专属名词，成名即Reputation，Reputation即成名
 * 定义rptn为Reputation的专有缩写。
 * 
 * @author hy
 * @date 2021年10月24日
 */
@Controller
public class ReputationCtrl {
	private final static Logger logger = LoggerFactory.getLogger(ReputationCtrl.class);
	
	@Autowired
	private ReputationService rptnService;
	
	@Autowired
	private ReputationMapper rptnMapper;
	
	/**
	 * 添加一个成名
	 * @param name
	 * @return
	 */
	@PostMapping(value="/reputation/AddOne")
	public ModelAndView addReputation(String name,HttpSession session) {
    	User user = (User)session.getAttribute("user");
		
		boolean added=rptnService.addReputation(name,user.getId());
		
		if(added) {
			logger.info("Reputation:{} added.",name);
			
			session.setAttribute("added", name+"已被增添至数据库.");
			//return new ModelAndView("redirect:/reputation/List");
			return new ModelAndView("redirect:/reputation/gotoAddonelaterPage");
		}else {
			logger.info("Add reputation:{} failed.",name);
			ModelAndView mav=new ModelAndView("reputation/addOne");
			mav.addObject("errMsg", "影星成名：'"+name+"'已经存在了。");
			return mav;
		}
	}
	
	/**
	 * 添加成名及其简介
	 * @param name
	 * @param brief
	 * @param session
	 * @return
	 */
	@PostMapping(value="/reputation/AddOne2")
	public ModelAndView addReputationAndBrief(String name,String brief,HttpSession session) {
    	User user = (User)session.getAttribute("user");
		
		boolean added=rptnService.addReputation(name,brief,user.getId());
		
		if(added) {
			logger.info("Reputation:{} & brief{} added.",name,brief);
			return new ModelAndView("redirect:/reputation/List");
		}else {
			logger.info("Add reputation:{} & brief{} failed.",name,brief);
			ModelAndView mav=new ModelAndView("reputation/addOne");
			mav.addObject("errMsg", "影星成名：'"+name+"'已经存在了。");
			return mav;
		}
	}
	
	/**
	 * 成名列表
	 * @return
	 */
	@GetMapping(value="/reputation/List")
	public ModelAndView listReputations() {
		ModelAndView mav=new ModelAndView("reputation/list");
		List<String> ls=rptnMapper.queryAllNames();
		logger.info("found {} reputations.",ls.size());
		
		mav.addObject("rptns_01", getSublist(ls, 0, 25));
		mav.addObject("rptns_02", getSublist(ls, 26, 50));
		mav.addObject("rptns_03", getSublist(ls, 51, 75));
		mav.addObject("rptns_04", getSublist(ls, 76, 100));
		
		return mav;
	}
	
	/**
	 * 从destlist中掐一节
	 * @param destList
	 * @param start
	 * @param end
	 * @return
	 */
	private static List<String> getSublist(List<String> destList,int start,int end){
		List<String> retval=new ArrayList<String>();
		
		for(int i=0;i<destList.size();i++) {
			if(start<=i && i<end) {
				retval.add(destList.get(i));
			}
		}
		
		return retval;
	}
	
	/**
	 * 添加多个成名
	 * @param multifnames
	 * @param delimiter
	 * @return
	 */
	@PostMapping(value="/reputation/AddMore")
	public ModelAndView addMoreRptn(String multifnames,String delimiter,HttpSession session) {
		User user = (User)session.getAttribute("user");
		int count=rptnService.addMultiReputation(multifnames, delimiter,user.getId());
		logger.info("{} reputations were added.",count);
		return new ModelAndView("redirect:/reputation/List");
	}
}
